<template>
	<view class="wrap" :style="{ height: windowHeight + 'px' }">
		<view class="card">
			<view class="title">
				<input placeholder="请描述反馈的标题" maxlength="20" v-model="title"></input>
			</view>
			<view class="content">
				<textarea v-model="content" placeholder="在这里输入问题描述内容" height="130" focus
					:adjust-position="false"></textarea>
				<view class="content_upload">
					<view class="u-list-item" hover-stay-time="150" style="width: 200rpx;height: 200rpx;"
						v-if="!contentAvatarUrl" @click="uploadImg">
						<u-icon name="plus" class="u-add-btn" size="40"></u-icon>
						<view class="u-add-tips">选择图片</view>
					</view>
					<view class="img" v-else>
						<view class="del" @click="clearImg">
							<u-icon name="close" class="u-add-btn" size="30" color="red"></u-icon>
						</view>
						<image class="u-avatar" :src="contentAvatarUrl" mode="aspectFill"></image>
					</view>
				</view>
			</view>
		</view>
		<button type="primary" class="btn" :disabled="disabled" @click="submit">提交</button>
	</view>
</template>

<script>
	import * as cloudApi from '@/utils/cloudApi.js'
	export default {
		data() {
			return {
				windowHeight: null,
				disabled: false,
				userInfo: {},
				colors: null,
				title: '',
				content: '',
				contentAvatarUrl: '',
				imgs: ''
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.colors
			})
		},
		mounted() {
			this.windowHeight = getApp().globalData.windowHeight
			this.userInfo = uni.getStorageSync("userInfo") || getApp().globalData.userInfo
			this.colors = this.userInfo.themeColors || '#F3C301'
		},
		methods: {
			clearImg() {
				console.log(1);
				this.contentAvatarUrl = ''
			},
			uploadImg() {
				uni.chooseImage({
					count: 1,
					success: async res => {
						console.log(res);
						if (res.tempFilePaths.length > 0) {
							this.contentAvatarUrl = res.tempFilePaths[0]
						}
					}
				});
			},
			async submit() {
				if (!this.content || !this.title) return this.$showToast('不能为空', 'none')
				if (this.contentAvatarUrl) {
					let time = this.$u.timeFormat(Date.now(), 'MM分ss秒');
					const result = await uniCloud.uploadFile({
						filePath: this.contentAvatarUrl,
						cloudPath: this.userInfo.nickName + '_' + time + '_feedBack.jpg'
					});
					this.imgs = result.fileID
				}

				cloudApi.call({
					name: "updateUserInfo",
					data: {
						api: 'submitFeedback',
						title: this.title,
						content: this.content,
						contentAvatarUrl: this.imgs,
						openid: this.userInfo.openid
					},
					success: res => {
						this.$showToast('感谢您的反馈！', 'none')
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					}
				})
			},

		},
		create() {
			// 微信小程序需要用此写法

		},
		computed: {

		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		background: #f5f5f5;

		.card {
			background: #fff;
			padding-bottom: 30rpx;
		}

		.title {
			padding: 20rpx 0;
			margin: 0 20rpx 20rpx;
			border-bottom: 0.5rpx solid #eee;
		}

		.content {
			padding: 20rpx;

			.content_upload {
				.u-list-item {
					width: 200rpx;
					height: 200rpx;
					overflow: hidden;
					margin: 10rpx;
					background: rgb(244, 245, 246);
					position: relative;
					border-radius: 10rpx;
					/* #ifndef APP-NVUE */
					display: flex;
					/* #endif */
					align-items: center;
					justify-content: center;
					flex-direction: column;
					color: $u-content-color;
					font-size: 26rpx;

					.u-add-tips {
						margin-top: 20rpx;
						line-height: 40rpx;
					}
				}

				.img {
					position: relative;

					.del {
						position: absolute;
						left: 170rpx;
						margin: 10rpx;
					}

					image {
						width: 200rpx;
						height: 200rpx;
						margin: 10rpx;
						border-radius: 10rpx;
					}
				}

			}
		}

		.btn {
			margin-top: 30rpx;
			height: 90rpx;
			color: #ffffff;
			border-radius: 20rpx;
			text-align: center;
			margin: 20rpx;
		}
	}
</style>
